#-*-Makefile-*- vim:syntax=make
#
# Copyright (c) 2013 OSW. All rights reserved.
# Copyright (c) 2008-2012 the MansOS team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#  * Redistributions of source code must retain the above copyright notice,
#    this list of  conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#
#	GCC compiler related options, used by many but not all platforms
#

ifeq ($(OPTIMIZE),yes)
	CFLAGS += -Os
endif

GCC_VERSION := $(shell $(CC) -v 2>&1 | grep "gcc version" | cut -b 13)
GCC_SUBVERSION := $(shell $(CC) -v 2>&1 | grep "gcc version" | cut -b 15)

ifeq ($(USE_REPROGRAMMING),y)
	OPTIMIZE_SIZE_EXTRA ?= no
endif
ifeq ($(PLATFORM),pc)
	OPTIMIZE_SIZE_EXTRA ?= no
endif
#ifeq ($(UNAME),Darwin)
# --gc-sections not available on MacOSX GCC   /really?/
#	OPTIMIZE_SIZE_EXTRA ?= no
#endif
ifeq ($(USE_RAM_EXECUTION),y)
	OPTIMIZE_SIZE_EXTRA ?= no
endif

ifeq (4,$(GCC_VERSION))
# Enable size optimizations by default on GCC 4.x where x < 5.
# Note: some versions of msp430-gcc 4.5 and 4.6 have the --gc-sections bug fixed,
# e.g. the one in Ubuntu 12.04. Unfortunately, it's too hard to discern which is which...
ifeq ("", "$(filter $(GCC_SUBVERSION), 5 6)")
	OPTIMIZE_SIZE_EXTRA ?= yes
endif
endif

ifeq (yes,$(OPTIMIZE_SIZE_EXTRA))
	CFLAGS += -fdata-sections -ffunction-sections
	LDFLAGS += -Wl,--gc-sections
endif

CFLAGS += -Wno-unused-but-set-variable

ifeq (4,$(GCC_VERSION))
ifneq ("", "$(filter $(GCC_SUBVERSION), 6 7)")
NEW_GCC ?= 1
endif
endif
NEW_GCC ?= 0
